package com.smart.wap.h5;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.smart.common.entities.ajax.AjaxResult;
import com.smart.common.entities.constant.SmartConstant;
import com.smart.common.entities.core.BaseSearch;
import com.smart.common.entities.core.Page;
import com.smart.manager.member.entities.models.MemberProfileAddress;
import com.smart.manager.member.repository.MemberProfileAddressRepository;
import com.smart.utils.MemberAcl;
import com.smart.utils.MemberStp;
import com.smart.wap.bodys.WapMemberBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会员资料信息接口
 *
 * @author Administrator
 */
@Api(tags = "移动端-收货地址")
@RestController
@RequestMapping(SmartConstant.WAP_API_PREFIX + "h5/address")
@RequiredArgsConstructor
@Log4j2
public class AddressController {

    private final MemberProfileAddressRepository addressRepository;

    @ApiOperation(value = "查询收货地址列表")
    @GetMapping(value = "pageList")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<Page<MemberProfileAddress>> getList(@Validated BaseSearch search) {
        Page<MemberProfileAddress> result = addressRepository.getPageList(MemberAcl.getLoginId(), search);
        return AjaxResult.data(result);
    }

    @ApiOperation(value = "查询收货地址列表")
    @GetMapping(value = "getAllAddress")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<List<MemberProfileAddress>> getAllAddress() {
        List<MemberProfileAddress> result = addressRepository.getAllAddress(MemberAcl.getLoginId());
        return AjaxResult.data(result);
    }


    @ApiOperation(value = "新增收货地址")
    @PostMapping(value = "add")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<String> saveAddress(@RequestBody @Validated WapMemberBody.AddressBody body) {

        addressRepository.saveAddr(MemberAcl.getLoginId(), body);
        return AjaxResult.success();
    }

    @ApiOperation(value = "修改收货地址", notes = "包含有一个路径参数 用户地址")
    @PostMapping(value = "update/{addressId}")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<String> updateAddress(@PathVariable Long addressId, @RequestBody @Validated WapMemberBody.AddressBody body) {
        addressRepository.updateMemberAddr(MemberAcl.getLoginId(), addressId, body);
        return AjaxResult.success();
    }


    @ApiOperation(value = "设置默认地址", notes = "路径参数 用户地址")
    @PostMapping(value = "setDefaultAddr/{addressId}")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<String> setDefaultAddr(@PathVariable Long addressId) {
        addressRepository.setDefaultAddr(MemberAcl.getLoginId(), addressId);
        return AjaxResult.success();
    }

    @ApiOperation(value = "删除收货地址", notes = "路径参数 用户地址")
    @PostMapping(value = "delete/{addressId}")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<String> delAddress(@PathVariable Long addressId) {
        addressRepository.delMemberAddr(MemberAcl.getLoginId(), addressId);
        return AjaxResult.success();
    }


}
